$('.right-titleicon').on("click", function(e) {
	projectVM.showMenulist()
})
 
projectVM = new Vue({
	el: "#content",
	data: {
		approveDesc: '',
		approveDialogTips: true,
		isApprove: false,
		isMyEditor: false,
		workStatus: '20',
		dealingPerson: 'P129951',
		//dealingPerson: 'P129952',
		// 是否是编辑
		isEdit: false,
		// 编辑对应行
		index: 0,
		approveCodeName: '',
		workDesc: '网络安全',
		workType: '0115',
		// 流程id
		workId: "67e09d698e8e4df8b1e4f1ccc290eaa4", // 获取流程日志所需待办Id
		addOrEdit: false,
		isEditor: false,
		initNetSafeDeviceInfoList: [],
		formData: {
			networkSafeInfoId: '', // 网络安全防护设备检查明细主键
			networkSafeId: '', // 网络安全防护设备检查批次主键
			checkDate: 123,
			checkStaffCodeName: 'alwin', // 新增时测试人为当前登陆人
			checkStaffCode: 'p123123',
			checkStaffName: 'asd',
			deviceName: '', //设备名称
			bay: '', // 所属间隔
			isWarning: '0', // 默认不告警
			isAmbientNormal: '1', // 默认环境正常
			isHaveAbnormalDevice: '0', // 默认无不正常设备
			problemLog: '' // 问题记录
		},
		onStatus: false,
		showPop: false,
		showMenu: false,
		showAdd: false,
		elseDesc: '',
		//删除数据
		delNetSafeDeviceInfoList: [],
		networkSafe: {
			"networkSafeId": "",
			"workId": "",
			"workDesc": "网络安全防护设备定期检查",
			"elseDesc": ""
		},
		defectInfoList: [],
		menuList: [{
			name: '新增',
			path: 'list.html'
		}, {
			name: '列表',
			path: 'index.html'
		}, ],
		filterText: 'all', //筛选条件
		list: [],
		currentUser: {},
		// 基本信息
		baseForm: {},
		baseRules: {
			checkStaff: [],
			checkDate: []
		},
		// 风电场列表
		wTableLoading: false, // 列表Loading
		windFarmForm: {
			list: []
		},
		deleteList: [], // 删除的风电场列表
		windFarmRules: {
			stationName: [{
				required: true,
				message: '请输入风场',
				trigger: 'blur'
			}]
		},
		// 巡检信息
		cTableLoading: false, // 列表Loading
		detailList: [],
		checkForm: {
			list: []
		},
		myUid: '',
		isEditAdd: false,
		// 数据字典
		yesOrNo: [] // 是否
	},
	components: {
		personselect,
		processlog
	},
	created() {
		console.log(JSON.parse(localStorage.getItem('neiosUser')))
		console.log(localStorage.getItem('todoWork'))
		this.workId = JSON.parse(localStorage.getItem('todoWork')).obj.workId
		this.workStatus = JSON.parse(localStorage.getItem('todoWork')).obj.workStatus
		this.currentUser = JSON.parse(localStorage.getItem('neiosUser'))
		if (this.workStatus == '20') {
			this.isMyEditor = true

		}
		let dictCodes = 'YES_OR_NO';
		let dict = mui.neiosCommon.queryDict(dictCodes, (data) => {
			if (data != '') {
				this.yesOrNo = data.YES_OR_NO;
				console.log(JSON.stringify(this.yesOrNo))
			}
		})
		this.getStationCode()
		this.getWindFarmCheckData()
		
	},
	beforeDestroy() {},
	methods: {
		//选择场站
			showStationList(index){
				var that = this
				 let picker = new mui.PopPicker({
					 layer:1
				 })
				 picker.setData(this.stationList)
				  picker.show(function(e) {   //e 就是总数据的数组 显示picker
					that.windFarmForm.list[index].stationCode = e[0].value
					that.windFarmForm.list[index].stationName = e[0].text
				})
			
			},
		//获取场站列表
		getStationCode(){
			var that = this
			var param = {
				orgType:'2',
				parentOrgCode:this.currentUser.companyNewCode,
				parentOrgType:'1'	
			}
			mui.neiosRequst.httpRequst('/common/organization/getOrganizationListByType', param,
				(data)=> {
				if(data.code ==  0){	
					let arr = []
					data.data.forEach(item=>{
						if(item.newOrgName != '' && item.newOrgName != null){
							arr.push({
								text:item.newOrgName,
								value:item.newOrgCode
							})
						}
						
					})
					that.stationList = arr
					console.log(JSON.stringify(arr))
				}
				else{
					mui.toast(data.msg)
				}
			
				},
				function(data) {
				console.log(JSON.stringify(param))
				})
		},	
		// 获取风电场巡检数据
		getWindFarmCheckData() {
			let _this = this;
			let param = {
				workId: this.workId,
				newAreaCode: this.currentUser.companyNewCode
			};
			
			mui.neiosRequst.httpRequst('/app/windFarmCheck/getWindFarmCheckData', param,
				(data) => {
					
					if (data.baseInfo == null) {
						_this.baseForm = {
							checkId: mui.neiosCommon.getUuidv4(),
							workId: _this.workId,
							newAreaCode: _this.currentUser.companyNewCode,
							checkCode: _this.currentUser.userId,
							checkName: _this.currentUser.name,
							checkStaff: '[' + _this.currentUser.userId + ']' + _this.currentUser.name,
							checkDate: this.getTime(new Date(), 'min'),
							biztypeId: 'RWP'
						};
						_this.windFarmForm.list = data.windFarmList.map((item, index) => {
							item.checkInfoId = mui.neiosCommon.getUuidv4(),
								item.checkId = _this.baseForm.checkId,
								item.biztypeId = 'RWP',
								item.sort = index + 1;
							return item;
						});
					} else {
						_this.baseForm = data.baseInfo;
						_this.windFarmForm.list = data.windFarmList.map((item, index) => {
							item.sort = index + 1;
							return item;
						});
					}
					_this.detailList = data.detailList;

				},
				(data) => {
					mui.toast("获取数据错误：" + data.msg);
				}
			);
		},
		clickStop() {

		},
		//判断是否显示办理
		// isShowHandle() {
		// 	if (this.workStatus == '30') {
		// 		return true
		// 	} else {
		// 		return false
		// 	}
		// },
		// //判断是否显示编辑
		// isShowEdit() {
		// 	if (this.workStatus == '20') {
		// 		return true
		// 	} else {
		// 		return false
		// 	}
		// },
		// 删除
		del(item, index) {
			var btnArray = ['取消', '确定'];
			mui.confirm('是否确认删除？', '提示', btnArray, (e) => {
				if (e.index == 1) {
					this.list.splice(index, 1)
					if (item.networkSafeInfoId != null && typeof(item.networkSafeInfoId) != "undefined" && item.networkSafeInfoId !=
						"") {
						this.delNetSafeDeviceInfoList.push(item);
					}
				} else {

				}
			})

		},
		// 新增添加
		addList() {
			console.log(this.isEditAdd)
			if (this.isEdit) {
				console.log(JSON.stringify(this.checkForm.list))
				let changeIndex = 0
				let myCheckForm = JSON.parse(JSON.stringify(this.checkForm.list))
				if (this.isEditAdd) {
					this.detailList = this.detailList.concat(this.checkForm.list);
				} else {
					this.detailList.map((v, i) => {
						myCheckForm.map((value, index) => {
							if (value.checkDetailId == v.checkDetailId) {
								this.detailList.splice(i, 1, JSON.parse(JSON.stringify(value)))
							}
						})
					})
				}

			} else {
				this.windFarmForm.list.push({
					checkInfoId: this.myUid,
					checkId: this.baseForm.checkId,
					biztypeId: 'RWP',
					stationName: '',
					sort: this.windFarmForm.list.length + 1
				});
				this.detailList = this.detailList.concat(this.checkForm.list);
			}
			this.addOrEdit = false
		},
		delWindFarm(row) {
			var btnArray = ['取消', '确定'];
			mui.confirm('是否确认删除？', '提示', btnArray, (e) => {
				if (e.index == 1) {
					if (row.dataType == 'db') {
						row.dataStatus = '0';
						this.deleteList.push(row);
					}
					this.detailList.map(item => {
						if (item.checkInfoId == row.checkInfoId) {
							item.dataStatus = '0';
						}
					});
					let currIndex = -1;
					this.windFarmForm.list = this.windFarmForm.list.filter((item, index) => {
						if (item.checkInfoId == row.checkInfoId) {
							currIndex = index;
						}
						return item.checkInfoId != row.checkInfoId;
					}).map((item, index) => {
						item.sort = index + 1;
						return item;
					});
				} else {

				}
			})
			// let lastIndex = this.windFarmForm.list.length - 1
			// currIndex = lastIndex < currIndex ? lastIndex : currIndex;
			// currIndex > -1 ? this.$refs.windFarmTable.setCurrentRow(this.windFarmForm.list[currIndex]) : this.checkForm.list = [];
		},
		// 清楚编辑信息
		clearFormateData() {
			this.addOrEdit = false
		},
		add() {
			this.isEdit = false
			this.addOrEdit = true;
			this.checkForm.list = []
			this.myUid = mui.neiosCommon.getUuidv4();
			['因温度停机台数', '风速功率是否匹配', '限功率风机台数', '报警运行台数',
				'故障检修台数', '定期维护台数', '无通迅台数', '特殊关注风机'
			].map((item, index) => this.checkForm.list.push({
				checkDetailId: mui.neiosCommon.getUuidv4(),
				checkInfoId: this.myUid,
				checkContent: item,
				result: '',
				sort: index + 1,
				checkType: '00'
			}));

		},
		//新巡视人若编辑了历史信息则对应巡视人为新巡视人否则保持为原巡视人
		changeCheck(e) {
			this.formData.checkStaffCodeName = this.currentUser.userName
			this.formData.checkStaffCode = this.currentUser.userId;
			this.formData.checkStaffName = this.currentUser.name;

		},
		edit(row) {
			this.isEdit = true
			this.addOrEdit = true
			this.isEditAdd = false
			this.checkForm.list = JSON.parse(JSON.stringify(this.detailList.filter((item, index) => item.checkInfoId == row.checkInfoId)))
			if (this.checkForm.list.length == 0) {
				this.isEditAdd = true;
				['因温度停机台数', '风速功率是否匹配', '限功率风机台数', '报警运行台数',
					'故障检修台数', '定期维护台数', '无通迅台数', '特殊关注风机'
				].map((item, index) => this.checkForm.list.push({
					checkDetailId: mui.neiosCommon.getUuidv4(),
					checkInfoId: row.checkInfoId,
					checkContent: item,
					result: '',
					sort: index + 1,
					checkType: '00'
				}));
			}
		},
		//获取时间
		getTime(myDate, type) {
			console.log(type)
			var realYear = myDate.getFullYear();
			var myMonth = myDate.getMonth() + 1;
			var realMonth = myMonth;
			var myDay = myDate.getDate();
			var realDay = myDay;
			var myHour = myDate.getHours();
			var realHours = myHour;
			var myMinute = myDate.getMinutes();
			var realMinute = myMinute;
			var mySecond = myDate.getSeconds();
			var realSecond = mySecond;
			if (myMonth < 10) {
				realMonth = "0" + myMonth;
			}
			if (myDay < 10) {
				realDay = "0" + myDay;
			}
			if (myMinute < 10) {
				realMinute = "0" + myMinute;
			}
			if (myHour < 10) {
				realHours = "0" + myHour;
			}
			if (mySecond < 10) {
				realSecond = "0" + mySecond;
			}
			var realDate = realYear + '-' + realMonth + '-' + realDay + ' ' + realHours + ':' + realMinute + ':' +
				realSecond;
			if (type == "sec") {
				realDate = realYear + '-' + realMonth + '-' + realDay + ' ' + realHours + ':' + realMinute + ':' + realSecond;
			} else if (type == "min") {
				realDate = realYear + '-' + realMonth + '-' + realDay + ' ' + realHours + ':' + realMinute;
			} else if (type == "hour") {
				realDate = realYear + '-' + realMonth + '-' + realDay + ' ' + realHours;
			} else if (type == "day") {
				realDate = realYear + '-' + realMonth + '-' + realDay;
			} else if (type == "month") {
				realDate = realYear + '-' + realMonth;
			} else if (type == "year") {
				realDate = realYear;
			}
			return realDate;
		},
		navTo(e, data) {
			if (data) {
				window.localStorage.setItem("itme", JSON.stringify(data))
			} else {
				window.localStorage.setItem("itme", '')
			}
			mui.openWindow({
				url: e,
				id: 'my',
				extras: {
					'item': data
				},
				waiting: {
					autoShow: false
				}
			});
		},
		// 清除隐患描述
		clearDefectInfo() {
			this.showAdd = false
			this.isApprove = false
			this.defectInfoList = []
			this.approveDesc = ''
		},
		// 初始化隐患描述
		initDefectInfo() {
			this.showAdd = true
			this.list.forEach(e => {
				if (e.isWarning != '0') {
					let defectInfoDesc = `网络安全防护设备定期检查：[${e.bay}]${e.deviceName}。检查项：是否有告警。不合格`;
					this.defectInfoList.push({
						"defectInfoDesc": defectInfoDesc
					});
				}
				if (e.isAmbientNormal != '1') {
					let defectInfoDesc = `网络安全防护设备定期检查：[${e.bay}]${e.deviceName}。检查项：运行环境是否正常。不合格`;
					this.defectInfoList.push({
						"defectInfoDesc": defectInfoDesc
					});
				}
				if (e.isHaveAbnormalDevice != '0') {
					let defectInfoDesc = `网络安全防护设备定期检查：[${e.bay}]${e.deviceName}。检查项：是否有非正常业务设备。不合格`;
					this.defectInfoList.push({
						"defectInfoDesc": defectInfoDesc
					});
				}
			})
		},
		// 初始化通过隐患描述
		approveInitDefectInfo(type) {
			if (type == 'approve') {
				this.approveDialogTips = true
			} else {
				this.approveDialogTips = false
			}
			this.isApprove = true
			this.list.forEach(e => {
				if (e.isWarning != '0') {
					let defectInfoDesc = `网络安全防护设备定期检查：[${e.bay}]${e.deviceName}。检查项：是否有告警。不合格`;
					this.defectInfoList.push({
						"defectInfoDesc": defectInfoDesc
					});
				}
				if (e.isAmbientNormal != '1') {
					let defectInfoDesc = `网络安全防护设备定期检查：[${e.bay}]${e.deviceName}。检查项：运行环境是否正常。不合格`;
					this.defectInfoList.push({
						"defectInfoDesc": defectInfoDesc
					});
				}
				if (e.isHaveAbnormalDevice != '0') {
					let defectInfoDesc = `网络安全防护设备定期检查：[${e.bay}]${e.deviceName}。检查项：是否有非正常业务设备。不合格`;
					this.defectInfoList.push({
						"defectInfoDesc": defectInfoDesc
					});
				}
			})
		},
		// 退回&&通过
		saveApprove() {
			//通过
			if (this.approveDialogTips) {
				let param = {
					"workId": this.workId,
					"workType": this.workType,
					"workDesc": this.workDesc,
					"companyCode": this.currentUser.companyCode,
					"stationCode": this.currentUser.stationCode,
					"approveDes": this.approveDesc
				};
				mui.neiosRequst.httpRequst('/app/regularWork/networkSafeDeviceCheck/approveNetworkSafeInfo', param,
					(data) => {
						if (data.code == '0') {
							mui.toast("通过成功");
							this.goBack()
						}
					},
					() => {
						console.log("通过失败!");
					}
				);

			} else {
				// 退回	
				if (this.approveDesc) {
					let param = {
						"workId": this.workId,
						"workType": this.workType,
						"nodeDealStaffCode": this.currentUser.userId,
						"nodeDealStaffName": this.currentUser.name,
						"approveDes": this.approveDesc,
						"nodeDealStaffRole": '0',
						"forwardNode": '1',
						"nodeVisibleRange": '20',
					};
					mui.neiosRequst.httpRequst('/app/personWork/sendBack', param,
						(data) => {
							if (data.code == '0') {
								mui.toast("退回成功");
								this.goBack()
							} else {
								mui.toast("退回失败");
							}
						},
						() => {
							console.log("退回失败!");
						}
					);
				} else {
					mui.toast("请输入意见描述");
				}
			}
		},
		getName(data) {
			console.log(data)
			if (data) {
				let list = []
				data.split(',').map((v) => {
					list.push(v.split(']')[1])
				})
				return list.join(',');
			} else {
				return data
			}

		},
		// 保存
		save() {
			let _this = this;
			if (this.windFarmForm.list.length == 0) {
				mui.toast("请至少添加一个风场");
				return;
			}
			let param = {
				baseInfo: this.baseForm, // 基本信息
				windFarmList: this.windFarmForm.list, // 风电场列表
				deleteList: this.deleteList, // 删除的风电场列表
				detailList: this.detailList // 巡检信息列表
			};

			mui.neiosRequst.httpRequst('/app/windFarmCheck/saveWindFarmCheckData', param,
				(data) => {
					// this.goBack()
					mui.toast("保存成功");
					setTimeout(function() {
						location.reload();
					}, 1000);
				},
				(data) => {
					mui.toast("保存错误：" + data.msg);
				}
			);
		},
		// 提交
		submit() {
			let _this = this;
			
			if (this.windFarmForm.list.length == 0) {
				mui.toast("请至少添加一个风场");
				return;
			}
			let check = true;
			
			check = check && !this.windFarmForm.list.some((item, index) => {
				let checkList = this.detailList.filter(it => it.checkInfoId == item.checkInfoId);
				let checkFailed = checkList.length == 0 || checkList.some(it => it.result == '');
				if (checkFailed) {
					// this.$refs.windFarmTable.setCurrentRow(this.windFarmForm.list[index]);
					mui.toast(`请输入场站：${this.windFarmForm.list[index].stationName}内容`);
				}
				return checkFailed;
			});
			this.windFarmForm.list.map((v, i) => {
				if (!v.stationName) {
					mui.toast("请填写场站名称");
					check = false
					return
				}
			})
			if (check) {
				let param = {
					baseInfo: this.baseForm, // 基本信息
					windFarmList: this.windFarmForm.list, // 风电场列表
					deleteList: this.deleteList, // 删除的风电场列表
					detailList: this.detailList // 巡检信息列表
				};
				console.log(JSON.stringify(param))
				// return
				mui.neiosRequst.httpRequst('/app/windFarmCheck/submitWindFarmCheckData', param,
					(data) => {
						// this.goBack()
						mui.toast("提交成功");
						setTimeout(function() {
							_this.goBack()
						}, 1000);
					},
					(data) => {
						mui.toast("保存错误：" + data.msg);
					}
				);
			}
		},
		//返回上一页
		goBack() {
			let page2 = plus.webview.getWebviewById("../sitOperation/terminalwork/terminalwork.html");
			let param = {}
			param.isRefresh = true
			mui.fire(page2, 'isRefresh', param);
			mui.back()
		},
		//新巡视人若编辑了历史信息则对应巡视人为新巡视人否则保持为原巡视人
		changeCheck() {
			this.formData.checkStaffCode = this.currentUser['userId'];
			this.formData.checkStaffName = this.currentUser['name'];
			this.formData.checkStaffCodeName = this.currentUser['userName'];
		},
		yesNoChange() {
			console.log(this.formData.isWarning)
			let problemFlag;
			let log = "检查项目：";
			if (this.formData.isWarning != '0' || this.formData.isAmbientNormal != '1' || this.formData.isHaveAbnormalDevice !=
				'0') {
				problemFlag = true;
			} else {
				problemFlag = false;
			}
			if (this.formData.isWarning != '0') {
				log = `${log}是否有告警。`;
			}
			if (this.formData.isAmbientNormal != '1') {
				log = `${log}运行环境是否正常。`;
			}
			if (this.formData.isHaveAbnormalDevice != '0') {
				log = `${log}是否有非正常业务设备。`;
			}
			if (problemFlag) {
				log = `${log}异常`;
				this.formData.problemLog = log;
			} else {
				this.formData.problemLog = "";
			}
			this.changeCheck();

		},
		//点击菜单
		menuClick(e) {
			this.navTo(e.path)

		},
		//左上角菜单显示隐藏
		showMenulist() {
			this.showMenu ? this.showMenu = false : this.showMenu = true
		},
		//切换筛选条件
		changeFliter(e) {
			this.filterText = e
		},
		showPop() {
			this.show_popup = true
		},
		hidePopup() {
			this.show_popup = false
		},

	}

})
